package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.service.IOrderGoodsService;
import cn.leyaber.hrm.domain.OrderGoods;
import cn.leyaber.hrm.query.OrderGoodsQuery;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import cn.leyaber.hrm.redusutils.UserContext;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderGoods")
public class OrderGoodsController {

    @Autowired
    public IOrderGoodsService orderGoodsService;
    /**
     * 订单提交
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request) {
        try {
            String payData = orderGoodsService.submit(params,UserContext.getWholename(request));
            System.out.println(payData);
            return AjaxResult.me().setResultObj(payData);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("订单提交失败" +e.getMessage());
        }
    }

    /**
    * 保存和修改公用的
    * @param orderGoods  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody OrderGoods orderGoods){
        try {
            if(orderGoods.getId()!=null){
                orderGoodsService.updateById(orderGoods);
            }else{
                orderGoodsService.insert(orderGoods);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderGoodsService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public OrderGoods get(@PathVariable("id")Long id)
    {
        return orderGoodsService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<OrderGoods> list(){

        return orderGoodsService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<OrderGoods> json(@RequestBody OrderGoodsQuery query)
    {
        Page<OrderGoods> page = new Page<OrderGoods>(query.getPage(),query.getRows());
        page = orderGoodsService.selectPage(page);
        return new PageList<OrderGoods>(page.getTotal(),page.getRecords());
    }

}
